package com.mall.live.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbGoods;
import com.mall.entity.TbItem;
import com.mall.entity.TbUser;
import com.mall.entity.live.TbLive;
import com.mall.entity.live.TbLiveFollower;
import com.mall.entity.live.TbLiveProduct;
import com.mall.live.mapper.LiveMapper;
import com.mall.live.mapper.LiveProductMapper;
import com.mall.live.service.ILiveService;
import com.mall.live.vo.ItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author  LiJinYang
 * @email  283006176@qq.com
 * @date 2021/07/27 18:51
 * @version 1.0
 */
@Service
public class LiveServiceImpl implements ILiveService {

    @Autowired
    private LiveMapper liveMapper;
    @Autowired
    private LiveProductMapper liveProductMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表，分页
     * @param id
     * @return
     */
    @Override
    public Map<String,List<TbLive>> findAll(Integer id) {
        TbLive one = liveMapper.findOne(id);
        //查询出每次直播的观看人数
        List<TbLive> lookList = liveMapper.findLookNumList(one.getUsername());
        //查询出每次直播的时长
        List<TbLive> liveTimeList = liveMapper.findLiveTimeList(one.getUsername());
        //查询出每次直播的订单量
        List<TbLive> orderNum = liveMapper.findOrderNum(one.getUsername());
        Map<String,List<TbLive>> map = new HashMap<>(16);
        map.put("lookList",lookList);
        map.put("liveTimeList",liveTimeList);
        map.put("orderNum",orderNum);
        return map;
    }

    /**
     * 直播列表
     * @param tbLive
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findPage(TbLive tbLive, int page, int size) {
        PageHelper.startPage(page,size);
        List<TbLive> list = liveMapper.findPage(tbLive);
        return new PageInfo(list);
    }

    /**
     * 修改直播状态
     * @param id
     * @param status
     */
    @Override
    public void update(Long id, String status) {
        liveMapper.update(id,status);
    }

    /**
     * 执行逻辑删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        liveMapper.update(id,"2");
    }

    /**
     * 查看直播人,显示直播人、在线人数;
     * @param id
     * @return
     */
    @Override
    public CommonResponse liveDetails(Long id) {
        return liveMapper.liveDetails(id);
    }

    @Override
    public void update(int id, String status) {

    }

    @Override
    public CommonResponse updateStatusById(Integer id, String status) {
        //做修改之前要先计算一下直播时长
        TbLive live = liveMapper.findOne(id);
        long diff = System.currentTimeMillis()-live.getStartTime().getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
        String liveTime = hours+"小时"+minutes+"分";
        live.setStatus(status);
        live.setLiveTime(liveTime);
        live.setEndTime(new Date());
        liveMapper.updateStatusById(live);
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }

    @Override
    public TbLive findOne(Integer id) {
        return liveMapper.findOne(id);
    }

    @Override
    public CommonResponse addLive(TbLive live,Long[] itmes,String token) {
        TbUser user = (TbUser) redisTemplate.opsForValue().get(token);
        live.setUid(user.getId().longValue());
        live.setUsername(user.getName());
        live.setStartTime(new Date());
        live.setEndTime(null);
        live.setStatus("0");
        live.setSalary(new BigDecimal(0));
        live.setCreateTime(new Date());
        live.setLiveTime(null);
        live.setRole("1");
        //rtmp://10.3.176.3:1935/live/1推流地址，固定的， 1为自定义流名，用的用户ID，开启直播时返回给前台，自动拼接。
        live.setPushStream("rtmp://10.3.176.3:1935/live/");
        //rtmp://10.3.176.3/live/  拉流地址，每个人只有一个固定的拉流地址。
        live.setPullStream("rtmp://10.3.176.3/live/"+live.getUid());
        //先添加主直播表
        liveMapper.addLive(live);
        //添加直播商品表
        Arrays.asList(itmes).stream().forEach(item->{
            //根据商品id查询商品名称
            ItemVo it = liveMapper.findItem(item);
            TbLiveProduct liveProduct = new TbLiveProduct();
            liveProduct.setLid(live.getId());
            liveProduct.setItemId(item.longValue());
            liveProduct.setPrice(it.getPrice());
            liveProduct.setTitle(it.getGname());
            liveProductMapper.insert(liveProduct);
        });
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200,live.getId());
    }

    @Override
    public Map<String, List<TbLive>> findFeedBack(Integer id) {
        TbLive one = liveMapper.findOne(id);
        //查询出每次直播下单数量
        List<TbLive> orderNum = liveMapper.findOrderNum(one.getUsername());
        //查询出每次直播成交数量
        List<TbLive> saleNum = liveMapper.findSaleNumList(one.getUsername());
        Map<String, List<TbLive>> map = new HashMap<>(16);
        map.put("orderNum",orderNum);
        map.put("saleNum",saleNum);
        return map;
    }

    @Override
    public void liveInto(Long id) {
        liveMapper.updateLookNumById(id);
    }

    @Override
    public List<TbLive> findLiveList() {
        return liveMapper.findListList();
    }

    @Override
    public List<TbLive> findLiveRecommend() {
        return liveMapper.findLiveRecommend();
    }

    /**
     * 查询直播间观看人数
     * @param lid
     * @return
     */
    @Override
    public List<TbLiveFollower> watch(Long lid) {
        return liveMapper.watch(lid);
    }

    /**
     * 我的直播间
     * @return
     */
    @Override
    public List<TbLive> myLive(String token) {
        //根据token获取登录人信息
        TbUser user = (TbUser) redisTemplate.opsForValue().get(token);
        return liveMapper.myLive(user.getId());
    }

    /**
     * 开播，返回推流信息
     * @param id
     * @return
     */
    @Override
    public TbLive start(Integer id) {
        //获取直播间信息
        TbLive one = liveMapper.findOne(id);
        //存入redis，便于点赞关注
        redisTemplate.boundHashOps("live").put(one.getUid(),one);
        return liveMapper.start(id);
    }

    /**
     * 更新观看人数
     * @param id
     */
    @Override
    public TbLive liveNum(Integer id) {
        TbLive one = liveMapper.findOne(id);
        TbLive liveNum = (TbLive)redisTemplate.boundHashOps("live").get(one.getUid());
        liveNum.setLookNum(liveNum.getLookNum()+1);
        redisTemplate.boundHashOps("live").put(one.getUid(),liveNum);
        return liveNum;
    }
}
